# -*- coding:utf-8 -*-
from GoalObject import GoalObject
import rhinoscriptsyntax as rs

class KeepArea(GoalObject):
    calculate_arg =[]
    target_area=1
    def KeepArea(self,curve,x_scale): #init goal object
        #self.curve = curve
        self.target_area = rs.CurveArea(curve)[0]
        self.center = rs.CurveAreaCentroid(curve)[0]
        #print(self.center)
        points=rs.CurvePoints(curve)
        target_points =[]
        for p in points: #开始重新计算新的curve
            #print p
            delta=rs.PointSubtract(p,self.center)
            x,y=delta[0],delta[1]
            x=x*x_scale
            delta=(x,y,0)
            target_points.append(rs.PointAdd(delta,self.center))
        self.target=target_points
        self.curve=rs.AddPolyline(target_points)
        return self
    def Calculate(self):
        area=rs.CurveArea(self.curve)[0]
        y_scale=self.target_area/area
        points=rs.CurvePoints(self.curve)
        target_points =[]
        for p in points:
            delta=rs.PointSubtract(p,self.center)
            x,y=delta[0],delta[1]
            y=y*y_scale
            print('y_scale',y_scale)
            delta=(x,y,0)
            #print(delta)
            target_points.append(rs.PointAdd(delta,self.center))
        #print('KeepArea')
        self.target=target_points
        #print('target_points',target_points)
        self.curve=rs.AddPolyline(self.target)
        return self
